package com.google.code.meitt.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

public class PageTag extends TagSupport {
	private static List<String> javaScripts;
	private static List<String> css;
	
	static{
		javaScripts = new ArrayList<String>();
		javaScripts.add("scripts/jquery/jquery_1.7.1.js");
		javaScripts.add("scripts/main.js");
		javaScripts.add("scripts/ship.js");
		css= new ArrayList<String>();
		css.add("css/stiil.css");
	}
	private static final long serialVersionUID = 1L;
	private String title;
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.write("<!DOCTYPE html>\n");
			out.write("<html>\n");
			out.write("<head>\n");
			out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n");
			for(String file:css){
				out.write("<link type=\"text/css\" href=\"");
				out.write(file);
				out.write("\" rel=\"stylesheet\" />\n");
			}
			for(String js:javaScripts){
				out.write("<script type=\"text/javascript\" src=\"");
				out.write(js);
				out.write("\"></script>\n");
			}
			out.write("<title>");
			if(StringUtils.isNotBlank(title)){
				out.write(title);
			} else {
				out.write("MeiTT");
			}
			out.write("</title>\n");
			out.write("</head>\n");
			out.write("<body>\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().write("\n</body>\n</html>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}
